using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using net91com.iPhone;
using System.Net;
using System.IO;
using System.Xml;

namespace Installer
{
    public partial class Installer : Form
    {
        DataSet mSourceDS = new DataSet();
        public Installer()
        {
            InitializeComponent();
        }

        private void addEditToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Sources onew = new Sources();
            onew.ShowDialog();
            FillTree();
        }

        private void reloadAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            
        }
        private void FillTree()
        {
            FillSourceDataset();
            tvwApplications.Nodes.Clear();
            /*DataTable mSources = new DataTable();
            mSources.ReadXml(Application.ExecutablePath + ".set");
            foreach (DataRow dtRow in mSources.Rows)
            {
                TreeNode oNode = new TreeNode();
                oNode.Text= dtRow["Name"].ToString();
                oNode.Tag = dtRow["URL"].ToString();
                tvwApplications.Nodes.Add(oNode);
                ReloadSources(oNode);
            }
             */
        }
        private void ReloadSources(TreeNode tvwNode)
        {
            tvwNode.Nodes.Clear();
            //tvwNode.Tag 
        }
        private string GetXML(string sURL)
        {
            // used to build entire input
            StringBuilder sb = new StringBuilder();

            // used on each read operation
            byte[] buf = new byte[8192];

            // prepare the web page we will be asking for
            HttpWebRequest request = (HttpWebRequest)
                WebRequest.Create(sURL);

            // execute the request
            HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();

            // we will read data via the response stream
            Stream resStream = response.GetResponseStream();

            string tempString = null;
            int count = 0;

            do
            {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0)
                {
                    // translate from bytes to ASCII text
                    tempString = Encoding.ASCII.GetString(buf, 0, count);

                    // continue building the string
                    sb.Append(tempString);
                }
            }
            while (count > 0);
            return sb.ToString();
            //return sb.Remove(0,3).ToString();
        }
        private void FillSourceDataset()
        {
            if ( mSourceDS == null || mSourceDS.Tables.Count==0)
            {
                CreateDataTable();
            }
            tvwApplications.Nodes.Clear();
            DataTable mSources = new DataTable();
            mSources.ReadXml(Application.ExecutablePath + ".set");
            DataTable mSourcesData = mSourceDS.Tables["Source"];

            foreach (DataRow dtRow in mSources.Rows)
            {
                string sXML = GetXML(dtRow["URL"].ToString());
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(sXML);

                XmlNodeList xmlnode = xmldoc.GetElementsByTagName("info");
            }
        }
        private void CreateDataTable()
        {
            DataTable mTemp = new DataTable("Soruce");
            mTemp.Columns.Add("Name");
            mTemp.Columns.Add("Description");
            mTemp.Columns.Add("Maintainer");
            mTemp.Columns.Add("Contact");
            mTemp.Columns.Add("Url");
            mTemp.Columns.Add("Category");
            mSourceDS.Tables.Add(mTemp);

            mTemp = new DataTable("Items");
            mTemp.Columns.Add("SourceName");
            mTemp.Columns.Add("Category");
            mTemp.Columns.Add("BundleIdentifier");
            mTemp.Columns.Add("Name");
            mTemp.Columns.Add("Version");
            mTemp.Columns.Add("Location");
            mTemp.Columns.Add("Size");
            mTemp.Columns.Add("hash");
            mTemp.Columns.Add("url");
            mTemp.Columns.Add("description");
            mTemp.Columns.Add("date");
            mTemp.Columns.Add("Preflight");
            mTemp.Columns.Add("Install");
            mTemp.Columns.Add("Uninstall");
            mSourceDS.Tables.Add(mTemp);
        }
        private void Installer_Load(object sender, EventArgs e)
        {
            FillTree();
        }

        private void createPXLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataSet odata = new DataSet();
            odata.ReadXml("f:\\Dev\\Repo.xml");
            dataGridView1.DataSource = odata;
        }

    }
}